package com.mapforge.selection;

import java.awt.Point;
import java.awt.Rectangle;

/**
 * The {@link SelectionProportionalSizeModifier} class alters 2 points so that a
 * bounding rectangle defined from those points will have proportional width and
 * height according to a specified width and height.
 * 
 * @see AbstractSelectionModifier
 * @author Will Morrison
 * @since 1.0
 * @version 1.0
 */
public class SelectionProportionalSizeModifier extends
		AbstractSelectionModifier {
	private int propWidth;
	private int propHeight;

	public SelectionProportionalSizeModifier() {
		this(0, 0);
	}

	public SelectionProportionalSizeModifier(int proportionalWidth,
			int proportionalHeight) {
		this.propWidth = proportionalWidth;
		this.propHeight = proportionalHeight;
	}

	public void setProportionalWidth(int propWidth) {
		this.propWidth = propWidth;
	}

	public int getProportionalWidth() {
		return this.propWidth;
	}

	public void setProportionalHeight(int propHeight) {
		this.propHeight = propHeight;
	}

	public int getProportionalHeight() {
		return this.propHeight;
	}

	@Override
	public void doModify(Point startPoint, Point currentPoint) {
		// calculate difference in horizon and vertical
		Rectangle r = new Rectangle();
		int diffx = Math.abs(startPoint.x - currentPoint.x);
		int diffy = Math.abs(startPoint.y - currentPoint.y);
		int size = Math.min(diffx, diffy) + 1;
		diffx = (int) Math.max(Math.min((double) propWidth / propHeight, 1)
				* size, 1);
		diffy = (int) Math.max(Math.min((double) propHeight / propWidth, 1)
				* size, 1);
		r.setSize(diffx, diffy);
		// if (chkFromCenter.isSelected()) {
		// r.setLocation(p1.x - (diffx >> 1), p1.y - (diffy >> 1));
		// }
		// else
		if (currentPoint.x < startPoint.x) {
			// upper left
			if (currentPoint.y < startPoint.y) {
				r.setLocation(startPoint.x - diffx + 1, startPoint.y - diffy
						+ 1);
			}
			// bottom left
			else {
				r.setLocation(startPoint.x - diffx + 1, startPoint.y);
			}
		} else {
			// upper right
			if (currentPoint.y < startPoint.y) {
				r.setLocation(startPoint.x, startPoint.y - diffy + 1);
			}
			// bottom right
			else {
				r.setLocation(startPoint.x, startPoint.y);
			}
		}
		startPoint.setLocation(r.getLocation());
		currentPoint.setLocation(r.x + r.width, r.y + r.height);

	}

}
